package com.example.myaccounting.fragment.fragment_01;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.myaccounting.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 啦啦啦 on 2018/11/21.
 */

public class Fragme_01 extends Fragment implements View.OnClickListener {
    private View view;
    private TextView jz_tv_gr,jz_tv_jt;
    private ViewPager jz_viewpager;
    private List<Fragment>fragmenta=new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view =inflater.inflate(R.layout.fragme_01,container,false);
        setViews();
        setFrame();
        setListeners();

        return view;
    }

    private void setListeners() {
        jz_tv_gr.setOnClickListener(this);
        jz_tv_jt.setOnClickListener(this);
        jz_viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position==0){
                    efom(0);

                }else if(position==1){
                    efom(1);
                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setFrame() {
        fragmenta.add(new Fragme_011());
        fragmenta.add(new Fragme_012());
        InnerAdapter adapter = new InnerAdapter(getChildFragmentManager());
        jz_viewpager.setAdapter(adapter);

    }


    private void setViews() {
        jz_tv_gr=(TextView)view.findViewById(R.id.jz_tv_gr);
        jz_tv_jt=(TextView)view.findViewById(R.id.jz_tv_jt);
        jz_viewpager=(ViewPager)view.findViewById(R.id.jz_viewpager);
        jz_tv_gr.setTextColor(Color.parseColor("#d81e06"));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.jz_tv_gr:
                efom(0);
                break;
            case R.id.jz_tv_jt:
                efom(1);
                break;
        }

    }

    private void efom(int i) {
        if(i==0){
            jz_tv_gr.setTextColor(Color.parseColor("#d81e06"));
            jz_tv_jt.setTextColor(Color.parseColor("#707070"));
            jz_viewpager.setCurrentItem(i);
        }else if(i==1){
            jz_tv_gr.setTextColor(Color.parseColor("#707070"));
            jz_tv_jt.setTextColor(Color.parseColor("#d81e06"));
            jz_viewpager.setCurrentItem(i);

        }


    }

    class InnerAdapter extends FragmentPagerAdapter {
        public InnerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmenta.get(position);
        }

        @Override
        public int getCount() {
            return fragmenta.size();
        }
    }


}
